package com.xcity.game.employee.op;

import java.util.List;

import com.xcity.db.entity.Employee;
import com.xcity.db.entity.Player;
import com.xcity.game.common.AbstractConfirmOperation;
import com.xcity.game.common.Cause;
import com.xcity.game.common.GameStaticConfig;
import com.xcity.game.employee.EmployeeService;
import com.xcity.game.item.ItemTemplate;
import com.xcity.pomelo.push.msg.BatchItemsPush;

import naga.x.util.CommonUtils;

@Deprecated
public class FireOperation extends AbstractConfirmOperation {

	private EmployeeService service;
	private Employee employee;
	private ItemTemplate skillBook;
	private BatchItemsPush msgPush = new BatchItemsPush(2);
	
	public FireOperation(Player ref, Employee employee, EmployeeService service) {
		super(ref, null/*MessageFormat.format(MessageConst.EMPLOYEE_FIRE_HINT, gold)*/);
		this.employee = employee;
		this.service = service;
	}

	@Override
	public void ok(Player player) {
		if (skillBook != null && player.costGold(employee.getTemplate().getCompensation(), Cause.EMPLOYEE_FIRE)) {
			boolean succ = ref.getBag().addItemWithoutException(skillBook.getId(), 1, Cause.EMPLOYEE_FIRE);
			if (succ) {
				msgPush.add(skillBook, 1);
			}
		}
//		player.send(new TipPush(MessageConst.GOLD_NOT_ENOUGH));
		if (!msgPush.isEmpty()) {
			player.send(msgPush);
		}
	}

	@Override
	public void cancel(Player player) {
		if (skillBook != null && CommonUtils.randomInt(GameStaticConfig.defaultChanceBase) < employee.getTemplate().getSkillBookOdds()) {
			boolean succ = ref.getBag().addItemWithoutException(skillBook.getId(), 1, Cause.EMPLOYEE_FIRE);
			if (succ) {
				msgPush.add(skillBook, 1);
			}
		}
		if (!msgPush.isEmpty()) {
			player.send(msgPush);
		}
	}

	@Override
	public void send() {
//		EmployeeQuality quality = employee.getTemplate().getQuality();
//		int expBookId = quality.getExpBookId(employee.getLevel()); // e.getTemplate().getExpBookId();
//		if (expBookId > 0) {
//			boolean succ = ref.getBag().addItemWithoutException(expBookId, 1, Cause.EMPLOYEE_FIRE);
//			if (succ) {
//				msgPush.add(expBookId, 1);
//			}
//		}
		dropExpBooks();
		
//		EmployeeSkill passiveSkill = employee.getSkillByType(SkillTemplate.TYPE_PASSIVE);
//		boolean sent = false;
//		if (passiveSkill != null
//				&& (skillBook = service.configure().getCommonConfigure().getSkillBook(passiveSkill.getId())) != null) {
//			if (employee.getTemplate().getCompensation() > 0) {
//				msg = MessageFormat.format(MessageConst.EMPLOYEE_FIRE_HINT, employee.getTemplate().getCompensation());
//				super.send();
//				sent = true;
//			} else if (CommonUtils.randomInt(GameStaticConfig.defaultChanceBase) < employee.getTemplate().getSkillBookOdds()) {
//				boolean succ = ref.getBag().addItemWithoutException(skillBook.getId(), 1, Cause.EMPLOYEE_FIRE);
//				if (succ) {
//					msgPush.add(skillBook, 1);
//				}
//			}
//		}
//		if (!sent && !msgPush.isEmpty()) {
//			ref.send(msgPush);
//		}
	}
	
	private void dropExpBooks() {
		List<ItemTemplate> books = service.configure().getCommonConfigure().getExpBooks();
		if (books == null || books.isEmpty()) {
			return;
		}
		int exp = (int) (employee.getExp() * 0.9f); // 返90%经验
		
		if (exp < books.get(0).getParameter(0)) { // 0级员工或所返经验低于最低经验书的值
//			int expBookId = employee.getTemplate().getQuality().getExpBookId(employee.getLevel());
//			if (expBookId > 0) {
//				boolean succ = ref.getBag().addItemWithoutException(expBookId, 1, Cause.EMPLOYEE_FIRE);
//				if (succ) {
//					msgPush.add(expBookId, 1);
//				}
//			}
			return;
		}
		
		// 查找最接近该经验值的经验书
		int index = -1;
		int start = 0, end = books.size() - 1;
		while (start <= end) {
			index = (start + end) >> 1;
			int bookExp = books.get(index).getParameter(0);
			if (exp < bookExp) {
				end = index - 1;
			} else if (exp > bookExp) {
				start = index + 1;
			} else {
				break;
			}
		}
		if (index < 0) {
			return;
		}
		// 向前遍历计算掉落经验书数量
		for (int i = index; exp > 0 && i >= 0; i--) {
			ItemTemplate book = books.get(i);
			int bookExp = book.getParameter(0);
			int n = exp / bookExp;
			if (n > 0) {
				exp -= n * bookExp;
				boolean succ = ref.getBag().addItemWithoutException(book, 1, Cause.EMPLOYEE_FIRE);
				if (succ) {
					msgPush.add(book, 1);
				}
			}
		}
	}

}
